// pages/tx/tx.js
var comm = require("../module.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['请选择收款方式', '支付宝', '微信', '银行卡'],
    index: 0,
    curr: 0,
    user: "",
    yhk_status: true,
    bank_name: "",
    yhk:""
  },

  zf_change: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)

    if (e.detail.value == 3) {
      this.setData({
        yhk_status: false,
        user:"",

      })
    } else {
      this.setData({
        yhk_status: true,
        bank_name: "",
        yhk: ""
      })
      
    }
    this.setData({
      index: e.detail.value
    })
  },

  sb: function (e) {
    console.log(e)
    var val = e.detail.value
    if (this.data.yhk_status == true && val.user == "") {
      if (!comm.empty(val.user, "请输入账户名")) {
        return false
      }
    }
    // console.log(bank_name)
    if (this.data.yhk_status == false && val.bank_name == "") {
      if (!comm.empty(val.bank_name, "请输入银行卡名称")) {
        return false
      }
    }
    if (!comm.empty(val.rmb, "请输入金额")) {
      return false
    }


    if (val.zf_type < 0) {
      comm.toast("请选择收款方式")
      return false
    }

    if (this.data.yhk_status == false && val.yhk == "") {
      if (!comm.empty(val.yhk, "请输入银行卡卡号")) {
        return false
      }
    }

    if (val.rmb > this.data.curr) {
      comm.toast("您的余额不足")
      return false
    }

    console.log(val.zf_type)

    comm.ajax("Xcxflower/Cash", {
      money_num: val.rmb,
      ip: wx.getStorageSync("ph"),
      account: val.user,
      bank_account:val.yhk,
      bank_name: val.bank_name,
      account_type: val.zf_type,
      username: wx.getStorageSync("userName")
    }, res => {
      console.log(res)
      if (res.data.status != 10000) {
        comm.alert(res.data.message)
      } else {
        comm.alert("已成功提现，请等待")
        // console.log(val.zf_type)
       
        var cs = (val.zf_type + 1).toString()
        wx.setStorageSync("tx_type", cs)
        if(cs == 3){
          wx.setStorageSync("bank_name",val.bank_name)
          wx.setStorageSync("yhk",val.yhk)

          wx.setStorageSync("txzh", "")
        }else{
          wx.setStorageSync("txzh", val.user)

          wx.setStorageSync("bank_name", "")
          wx.setStorageSync("yhk", "")
        }
        console.log(cs)
     
        // console.log(wx.getStorageSync("tx_type"))

       
      }
    })

  },
  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (wx.getStorageSync("txzh") != "") {
      
    }
    console.log(wx.getStorageSync("zf_type"))
    if (wx.getStorageSync("tx_type") != "") {

      if (wx.getStorageSync("tx_type") == 3) {
        this.setData({
          index: wx.getStorageSync("tx_type"),
          bank_name: wx.getStorageSync("bank_name"),
          yhk: wx.getStorageSync("yhk"),
          yhk_status: false
        })
      }else{

        this.setData({
          user: wx.getStorageSync("txzh"),
          index: wx.getStorageSync("tx_type")
        })
      }
      // this.setData({
      //   index: wx.getStorageSync("tx_type")
      // })
      console.log(wx.getStorageSync("tx_type"))
    }

    

    var data = wx.getStorageSync("ph")
    comm.ajax("Xcxflower/getYue?ip=" + data, "", res => {
      console.log(res)
      if (res.data.status == 10000) {
        this.setData({
          curr: res.data.data.total_money
        })
      } else {
        comm.alert(res.data.message)
      }

    }, "GET")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})